package com.syscom.core.utils;

import com.syscom.common.entity.SysAccount;
import com.syscom.common.entity.SysUser;
import com.syscom.common.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * @author wjz
 * @date 2020/12/2 17:16
 * @Description 安全服务工具类
 */
@Component
public class SecurityUtils {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 获取Authentication
     */
    public Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前用户信息
     */
    public SysUser getCurrentUser() {
        SysAccount principal = (SysAccount) getAuthentication().getPrincipal();
        return sysUserMapper.selectById(principal.getUserId());
    }

    /**
     * 获取当前用户id
     */
    public Long getCurrentUserId() {
        SysAccount principal = (SysAccount) getAuthentication().getPrincipal();
        return principal.getUserId();
    }
}
